My attempt at trying to figure out how to do a Monte Carlo simulation on Python. When I was in High School, we actually had a lottery that would determine who would get a car parking pass for out senior year. So let's begin.

Let's first import the one python library that we need.

In [2]:
import random

This will be our class. In this scenario, we've got 300 students. You automatically get 1 ticket as long as you're a student. Obviously most people won't buy extra tickets (if you were like me and you didn't have a car anyways. On the lucky chance that I'd gotten picked, I would honestly have just sold it). Let's assume that some people buy an extra ticket or two but not a whole lot more. And then, there's the rich kid who decides to spend $1000 on this and get 50 times more than your average student. (I heard some people say a couple of people really did spend this much, but I graduated a long time ago). For simplicities sake, let's just pretend like 1 student has 50 times more than all the rest. Also embedded in the class is the masterpiece of probabilities: a randomizer. And, we'll also add in the class that if it's the rich kids number, we'll return a 1 (this isn't boolean, just a count of how many time rich kid is called).

In [3]:
class lottery():
    def __init__(self):
        self.student=[]
        for i in range (1,299):
            self.student.append(i)
        self.student.extend([300]*50)
        self.pick=None
    def pickStudent(self):
        self.pick=random.choice(self.student)
    def winner(self, numStudent):
        if str(numStudent) == str(self.pick):
            return 1
        else:
            return 0

Let's test our (or in this case rich kid's) luck and see the probability of getting picked.

In [4]:
def testMyLuck(numStudent, numPicks, game):
    numHits = 0
    for t in range(numPicks):
        game.pickStudent()
        numHits += game.winner(numStudent)
    probability = (numHits / numPicks)*100
    return probability

But, one simulation is just going to give you the sample probability. In order to get the true probability of the population, you'd want to run multiple simulations (law of large numbers).

In [5]:
game = lottery()
def simulation (numSim):
    probabilityArray = []
    for i in range(numSim):
        probabilityArray.append(testMyLuck(300,50,game))
    avgProbability = sum(probabilityArray)/len(probabilityArray)
    print ('The probablity that I will get chosen is', avgProbability, '%')
    
for simulations in (10, 100, 1000, 10000, 100000):
    print ('Number of simulations =', simulations)
    simulation(simulations)
Number of simulations = 10
The probablity that I will get chosen is 11.2 %
Number of simulations = 100
The probablity that I will get chosen is 14.74 %
Number of simulations = 1000
The probablity that I will get chosen is 14.454 %
Number of simulations = 10000
The probablity that I will get chosen is 14.454 %
Number of simulations = 100000
The probablity that I will get chosen is 14.36548 %

The true probability of this or 50/349 is approximately 14.326% and as you can see, the series does converge to the true probability the more iterations we have.